package ru.spb.osll.myfit.common.filter;

import org.junit.Test;
import ru.spb.osll.myfit.common.PageExecutingResult;

import java.util.HashMap;
import java.util.Map;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.easymock.EasyMock.*;

/**
 * User: Alexander Opeykin (alexander.opeykin@gmail.com)
 * Date: 5/1/12
 * Time: 4:55 PM
 */
public class MatchFilterTest {

  @Test
  public void testSerialization() throws Exception {
    MatchFilter filter = new MatchFilter("key", "value");
    assertEquals("key=value", filter.serialize());
  }

  @Test
  public void testSerializationWithoutValue() throws Exception {
    MatchFilter filter = new MatchFilter("key");
    assertEquals("key=", filter.serialize());
  }

  @Test
  public void testFilterTrue() throws Exception {
    String key = "key";
    String value = "value";
    Map<String, String> map = new HashMap<String, String>();
    map.put(key, value);
    PageExecutingResult mResult = createNiceMock(PageExecutingResult.class);
    expect(mResult.getMetadata()).andReturn(map).times(2);
    replay(mResult);

    MatchFilter filter = new MatchFilter(key, value);

    assertTrue(filter.filter(mResult));
    verify(mResult);
  }

  @Test
  public void testFilterNoSuchKey() throws Exception {
    PageExecutingResult mResult = createNiceMock(PageExecutingResult.class);
    expect(mResult.getMetadata()).andReturn(new HashMap<String, String>()).once();
    replay(mResult);

    MatchFilter filter = new MatchFilter("doesn't", "matter");

    assertTrue(!filter.filter(mResult));
    verify(mResult);
  }

  @Test
  public void testFilterWrongValue() throws Exception {
    String key = "key";
    Map<String, String> map = new HashMap<String, String>();
    map.put(key, "something");

    PageExecutingResult mResult = createNiceMock(PageExecutingResult.class);
    expect(mResult.getMetadata()).andReturn(map).times(2);
    replay(mResult);

    MatchFilter filter = new MatchFilter(key, "blahblahblah");

    assertTrue(!filter.filter(mResult));
    verify(mResult);
  }
}
